<?php

namespace app\store\controller;

use app\admin\common\Base;
use think\Db;

//序列号状态表
class Batchtracer extends Base
{

    public function index()
    {
        $data = $this->request->param();

        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            $list = [];
            // 入库
            $invpu = Db::name('invpu_goods')->alias('a')->join('invpu b', 'a.pid = b.id')->where(['a.is_del' => 0, 'a.enterprise_id' => $this->EnterpriseId])->limit($page, $limit / 2)->select();
            $count = Db::name('invpu_goods')->alias('a')->join('invpu b', 'a.pid = b.id')->where(['a.is_del' => 0, 'a.enterprise_id' => $this->EnterpriseId])->count();
            foreach ($invpu as $key => $value) {
                $goods = Db::name('goods')->where('id', $value['goods_id'])->find();
                $list[$key]['goods_id'] = $goods['id'];
                $list[$key]['goods_name'] = $goods['name'];
                $list[$key]['goods_model'] = $goods['model'];
                $list[$key]['goods_sn'] = $goods['sn'];
                $list[$key]['store_name'] = Db::name('store')->where('id', $value['store_id'])->value('name');
                $list[$key]['addtime'] = date('Y-m-d', $value['addtime']);
                switch ($value['o_type']) {
                    case 0:
                        $list[$key]['type'] = '采购订单';
                        break;
                    case 1:
                        $list[$key]['type'] = '采购单';
                        break;
                    case 2:
                        $list[$key]['type'] = '采购退货单';
                        break;
                    default:
                        # code...
                        break;
                }
                $list[$key]['customer_name'] = Db::name('customer')->where('id', $value['supplier_id'])->value('name');
                $list[$key]['sn'] = $value['sn'];
                $list[$key]['status'] = $value['check_status'] == 0 ? '已审核' : '未审核';
                $list[$key]['mark'] = $value['mark'];
            }

            // 出库
            $order = Db::name('order_good')->alias('a')->join('order b', 'a.order_id = b.id')->where(['a.is_del' => 0, 'a.enterprise_id' => $this->EnterpriseId])->where(['b.is_del' => 0, 'b.enterprise_id' => $this->EnterpriseId])->limit($page, $limit / 2)->select();
            $count += Db::name('order_good')->alias('a')->join('order b', 'a.order_id = b.id')->where(['a.is_del' => 0, 'a.enterprise_id' => $this->EnterpriseId])->where(['b.is_del' => 0, 'b.enterprise_id' => $this->EnterpriseId])->count();
            $i = count($list);
            foreach ($order as $key => $value) {
                $goods = Db::name('goods')->where('id', $value['good_id'])->find();
                $list[$i + $key]['goods_id'] = $goods['id'];
                $list[$i + $key]['goods_name'] = $goods['name'];
                $list[$i + $key]['goods_model'] = $goods['model'];
                $list[$i + $key]['goods_sn'] = $goods['sn'];
                $list[$i + $key]['store_name'] = Db::name('store')->where('id', $value['store_id'])->value('name');
                $list[$i + $key]['addtime'] = date('Y-m-d', $value['addtime']);
                switch ($value['o_type']) {
                    case 0:
                        $list[$i + $key]['type'] = '销售订单';
                        break;
                    case 1:
                        $list[$i + $key]['type'] = '销售单';
                        break;
                    case 2:
                        $list[$i + $key]['type'] = '销售退货单';
                        break;
                    default:
                        # code...
                        break;
                }
                $list[$i + $key]['customer_name'] = Db::name('customer')->where('id', $value['user_id'])->value('name');
                $list[$i + $key]['sn'] = $value['sn'];
                $list[$i + $key]['status'] = $value['status'] == 0 ? '已审核' : '未审核';
                $list[$i + $key]['mark'] = $value['mark'];
            }
            // halt($goods);
            return $this->grid_data($list, $count, $page, $limit);
        }

        return $this->fetch();
    }


    public function add()
    {
        $data = $this->request->param();

        return $this->fetch();
    }
}
